/**
 * 灯光管理
 */
$(document).ready(function(e) {	

	initialSpace();
	search(1);
	$("#searchSpace").change(function(){
		search(1);
	});
	initialClient();
	
	
})


function initialClient(){
   
        var clientString = localStorage.getItem('client');
        if(clientString){
        	clients = $.parseJSON(clientString);
        	var clientSelect = $("#searchClient");
            try{
                $.each(clients,function(index,data){

                        $('<option value="'+ data.id
                            +'">'+ (data.companyName != ''?data.companyName:data.memberName) +'</option>').appendTo($(clientSelect));
                });

            }catch(e){
                console.log(e.message);
            }
        }else{
        	$.ajax({
                url: '/euc/client/list',
                data:'',
                beforeSend: function(request) {
                    var accessToken = localStorage.getItem('accessToken');
                    request.setRequestHeader("accessToken",accessToken );
                },
                dataType: 'JSON',
                async: true,
                contentType:'application/json;charset=utf-8',
                type: 'POST',
                success: function (result, textStatus, response) {
                    var clientSelect = $("#searchClient");
                    try{
                        $.each(result,function(index,data){
                        		if(data.clientType=="企业"){
                        			$("#searchClient").append('<option value="'+ data.id+'">'+ (data.companyName != ''?data.companyName:data.memberName) +'</option>');
                        		}

                        });
 
                        var clientJson = JSON.stringify(clients);
                        localStorage.setItem('client',clientJson);
                    }catch(e){
                        console.log(e.message);
                    }
                },
                error: function () {
                    /*alert("error");*/
                }
            });
        }
        
   
    
}


function search(currentPage){
	var requestJson = getSearchParameter(currentPage);
	if(requestJson == false){
		return false;
	}
	$.ajax({
        url: '/euc/meetingRoomOrder/query',
        data:JSON.stringify(requestJson),
       // data:'{"data":{"status":-1,"startTime":"2017-12-31","endTime":"2018-12-31","spaceId":0},"currentPage":1}',
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	if(result.status === -2){
        		gotoLogin();
        	}
        	//alert(JSON.stringify(result));
        	pageBean = result.data;
        	$("#userTable").empty();	
        	
        	$.each(result.data.list,function(i,data){
        		       		

        		var bgcolor= localStorage.getItem('trcolor');
        		if(i % 2 == 0){
        			bgcolor = '';
        		}
        		var html = '<tr '+ bgcolor +'>';
        		html += '<td>' + data.spaceName + '</td>';
        		html += '<td>' + data.roomName + '</td>';
        		html += '<td>' + data.memberName + '</td>';
        		html += '<td>' + data.subject + '</td>';
        		html += '<td>' + data.beginTime +'</td>';
             	html += '<td><a href="javascript:edit('+data.id+');" class="tablelink">详情</a> <a href="javascript:del('+data.id+');" class="tablelink">详情</a></td>';
        		html += '</tr>';
	        	$("#userTable").append(html);
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');        	
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        	
        },
        error: function () {
        	console.log("error");
        }  
    });
}
function getSearchParameter(currentPage){
	try{
		var areaid = parseInt($("#searchSpace").val());

		//if(status==0) status=-1;
		if(!currentPage) currentPage=1;

		requestJson ={"data":{"spaceId":areaid},"currentPage":currentPage};
		return requestJson;
	}catch(e){
		console.log(e.messge);
	}
	return false;	
}